//
//  TabbarController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/27.
//

#import "TabbarController.h"
#import "DynamicViewController.h"
#import "FollowViewController.h"
#import "ShopViewController.h"
#import "MineViewController.h"
#import "BaseNaviViewController.h"

@interface TabbarController ()

@end

@implementation TabbarController

- (instancetype)init {
    UIEdgeInsets imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    UIOffset titleOffset = UIOffsetMake(0, 0);
    if (self = [super initWithViewControllers:[self viewControllersCreat] tabBarItemsAttributes:[self getTabBarItemsAttributes] imageInsets:imageInsets titlePositionAdjustment:titleOffset]) {
        [self customTabbarItems];
        self.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGRect frame = self.tabBar.frame;
    frame.size.height = [DeviceManager heightForTabbar];
    frame.origin.y = self.view.bounds.size.height - [DeviceManager heightForTabbar];
    self.tabBar.frame = frame;
}

- (NSArray *)viewControllersCreat {
    BaseNaviViewController *d = [[BaseNaviViewController alloc] initWithRootViewController:[[DynamicViewController alloc] init]];
    
    BaseNaviViewController *f = [[BaseNaviViewController alloc] initWithRootViewController:[[FollowViewController alloc] init]];
    
    BaseNaviViewController *s = [[BaseNaviViewController alloc] initWithRootViewController:[[ShopViewController alloc] init]];
    
    BaseNaviViewController *m = [[BaseNaviViewController alloc] initWithRootViewController:[[MineViewController alloc] init]];
    
    return @[d,f,s,m];
}

- (NSArray *)getTabBarItemsAttributes {
    NSDictionary *d = @{CYLTabBarItemTitle:@"动态",CYLTabBarItemImage:@"tabbar_dy",CYLTabBarItemSelectedImage:@"tabbar_dy_s"};
    NSDictionary *f = @{CYLTabBarItemTitle:@"关注",CYLTabBarItemImage:@"tabbar_follow",CYLTabBarItemSelectedImage:@"tabbar_follow_s"};
    NSDictionary *s = @{CYLTabBarItemTitle:@"店铺",CYLTabBarItemImage:@"tabbar_shop",CYLTabBarItemSelectedImage:@"tabbar_shop_s"};
    NSDictionary *m = @{CYLTabBarItemTitle:@"我的",CYLTabBarItemImage:@"tabbar_mine",CYLTabBarItemSelectedImage:@"tabbar_mine_s"};
    
    return @[d,f,s,m];
}

- (void)customTabbarItems {
    [self rootWindow].backgroundColor = [UIColor whiteColor];
    
    NSMutableDictionary *attrNormal = [NSMutableDictionary dictionary];
    attrNormal[NSForegroundColorAttributeName] = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:0.5];
    attrNormal[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    NSMutableDictionary *attrSelected = [NSMutableDictionary dictionary];
    attrSelected[NSForegroundColorAttributeName] = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
    attrSelected[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    [UITabBar appearance].translucent = NO;
    
    if (@available(iOS 13.0, *)) {
        UITabBarItemAppearance *appear = [[UITabBarItemAppearance alloc] init];
        [appear.normal setTitleTextAttributes:attrNormal];
        [appear.selected setTitleTextAttributes:attrSelected];
        
        UITabBarAppearance *tabbarAppear = [[UITabBarAppearance alloc] init];
        tabbarAppear.stackedLayoutAppearance = appear;
        tabbarAppear.backgroundColor = [UIColor whiteColor];
        tabbarAppear.shadowColor = [UIColor whiteColor];
        self.tabBar.standardAppearance = tabbarAppear;
        
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = tabbarAppear;
        }
    }else {
        UITabBarItem *tabbarItem = [UITabBarItem appearance];
        [tabbarItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
        [tabbarItem setTitleTextAttributes:attrSelected forState:UIControlStateSelected];
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    }
}

#pragma mark - delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    UIImpactFeedbackGenerator *feedback = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
    [feedback impactOccurred];
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control {
    UIView *v;
    if ([control cyl_isTabButton]) {
        v = [control cyl_tabImageView];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        CAKeyframeAnimation *ani = [CAKeyframeAnimation animation];
        ani.keyPath = @"transform.scale";
        ani.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.03,@1.0];
        ani.duration = 1.0;
        ani.repeatCount = 1;
        ani.calculationMode = kCAAnimationCubic;
        [v.layer addAnimation:ani forKey:nil];
    });
}

@end
